define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init();

            //绑定事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var panel = $($(this).attr("href"));
                if (panel.size() > 0) {
                    Controller.table[panel.attr("id")].call(this);
                    $(this).on('click', function (e) {
                        $($(this).attr("href")).find(".btn-refresh").trigger("click");
                    });
                }
                //移除绑定的事件
                $(this).unbind('shown.bs.tab');
            });

            //必须默认触发shown.bs.tab事件
            $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");
        },
        table: {
            one: function () {
                // 表格1
                var table1 = $("#table1");
                //当表格数据加载完成时
                table1.on('load-success.bs.table', function (e, data) {
                    //这里我们手动设置底部的值
                    $("#fans_total").text(data.fans_total);
                });
                table1.bootstrapTable({
                    url: 'user/fans/fans?inviter_id='+Config.inviter_id,
                    extend: {
                        index_url: '',
                        add_url: '',
                        edit_url: '',
                        del_url: '',
                        multi_url: '',
                        table: '',
                    },
                    toolbar: '#toolbar1',
                    pk: 'id',
                    sortName: 'id',
                    columns: [
                        [
                            {checkbox: true},
                            {field: 'id', title: __('Id'), sortable: true},
                            {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                            {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                            {field: 'avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                            {field: 'createtime', title: __('邀请时间'), operate: 'LIKE'},
                            {field: 'relation', title: __('关系'), operate: false},
                            {field: 'operate', title: __('Operate'), table: table1, events: Table.api.events.operate,
                                buttons: [
                                    {
                                        name: 'order',
                                        text: __('订单'),
                                        title: __('订单'),
                                        classname: 'btn btn-xs btn-primary btn-dialog',
                                        icon: 'fa',
                                        url: 'rush/order/index?buyer_id={id}'
                                    },
                                    {
                                        name: 'fans',
                                        text: __('粉丝'),
                                        title: __('粉丝'),
                                        classname: 'btn btn-xs btn-primary btn-addtabs',
                                        icon: 'fa',
                                        url: 'user/fans/index?inviter_id={id}'
                                    },
                                ],
                                formatter: Table.api.formatter.operate
                            }
                        ]
                    ]
                });

                // 为表格1绑定事件
                Table.api.bindevent(table1);
            },
            two: function () {
                // 表格2
                var table2 = $("#table2");
                //当表格数据加载完成时
                table2.on('load-success.bs.table', function (e, data) {
                    //这里我们手动设置底部的值
                    $("#order_total").text(data.total);
                    $("#order_price_total").text(data.price_total);
                });
                // 初始化表格
                table2.bootstrapTable({
                    url: 'user/fans/orderList?inviter_id='+Config.inviter_id,
                    extend: {
                        index_url: '',
                        add_url: '',
                        edit_url: '',
                        del_url: '',
                        multi_url: '',
                        table: '',
                    },
                    toolbar: '#toolbar2',
                    pk: 'id',
                    sortName: 'id',
                    columns: [
                        [
                            {checkbox: true},
                            {field: 'id', title: __('Id')},
                            {field: 'order_no', title: __('Order_no'), operate: 'LIKE'},
                            {field: 'order_status', title: __('Order_status'), searchList: {"0":__('Order_status 0'),"1":__('Order_status 1'),"2":__('Order_status 2'),"3":__('Order_status 3'),"4":__('Order_status 4'),"5":__('Order_status 5')}, formatter: Table.api.formatter.status},
                            {field: 'payment_voucher', title: __('Payment_voucher'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                            {field: 'complain_status', title: __('Complain_status'), searchList: {"0":__('Complain_status 0'),"1":__('Complain_status 1')}, formatter: Table.api.formatter.status},
                            {field: 'express_company', title: __('Express_company'), operate: 'LIKE'},
                            {field: 'express_no', title: __('Express_no'), operate: 'LIKE'},
                            {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                            {field: 'rushtime', title: __('Rushtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                            {field: 'goods_name', title: __('Goods_name'), operate: 'LIKE'},
                            {field: 'goods_price', title: __('Goods_price'), operate:'BETWEEN'},
                            {field: 'goods_image', title: __('Goods_image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                            {field: 'time_name', title: __('Time_name'), operate: 'LIKE'},
                            {field: 'seller.nickname', title: __('Seller.nickname'), operate: 'LIKE'},
                            {field: 'seller.mobile', title: __('Seller.mobile'), operate: 'LIKE'},
                            {field: 'buyer.nickname', title: __('Buyer.nickname'), operate: 'LIKE'},
                            {field: 'buyer.mobile', title: __('Buyer.mobile'), operate: 'LIKE'}
                        ]
                    ]
                });

                // 为表格2绑定事件
                Table.api.bindevent(table2);
            },
            three: function () {
                // 表格3
                var table3 = $("#table3");
                table3.bootstrapTable({
                    url: 'user/fans/statistic?inviter_id='+Config.inviter_id,
                    extend: {
                        index_url: '',
                        add_url: '',
                        edit_url: '',
                        del_url: '',
                        multi_url: '',
                        table: '',
                    },
                    toolbar: '#toolbar3',
                    pk: 'id',
                    sortName: 'id',
                    columns: [
                        [
                            {checkbox: true},
                            {field: 'id', title: __('Id'), sortable: true},
                            {field: 'user.nickname', title: __('User.nickname'), operate: 'LIKE'},
                            {field: 'user.mobile', title: __('User.mobile'), operate: 'LIKE'},
                            {field: 'user.avatar', title: __('User.avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                            {field: 'time_id', title: __('Time_id'), operate: 'IN', searchList: Config.time_list, formatter: Table.api.formatter.label},
                            {field: 'updatetime', title: __('进场时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        ]
                    ]
                });

                // 为表格2绑定事件
                Table.api.bindevent(table3);
            },
            four: function () {
                // 表格1
                var table4 = $("#table4");
                table4.bootstrapTable({
                    url: 'user/fans/rushSuccess?inviter_id='+Config.inviter_id,
                    extend: {
                        index_url: '',
                        add_url: '',
                        edit_url: '',
                        del_url: '',
                        multi_url: '',
                        table: '',
                    },
                    toolbar: '#toolbar4',
                    pk: 'id',
                    sortName: 'id',
                    columns: [
                        [
                            {checkbox: true},
                            {field: 'id', title: __('Id'), sortable: true},
                            {field: 'order_no', title: __('Order_no'), operate: false},
                            {field: 'goods_name', title: __('Goods_name'), operate: false},
                            {field: 'buyer.nickname', title: __('Buyer.nickname'), operate: 'LIKE'},
                            {field: 'buyer.mobile', title: __('Buyer.mobile'), operate: 'LIKE'},
                            {field: 'buyer.avatar', title: __('Buyer.avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                            {field: 'time_id', title: __('Time_id'), operate: 'IN', searchList: Config.time_list, formatter: Table.api.formatter.label},
                            {field: 'rushtime', title: __('抢购时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        ]
                    ]
                });

                // 为表格2绑定事件
                Table.api.bindevent(table4);
            }
        }
    };
    return Controller;
});